package com.bite.bite_forum.controller;
import com.bite.bite_forum.common.AppResult;
import com.bite.bite_forum.common.ResultCode;
import com.bite.bite_forum.exception.ApplicationException;
import com.bite.bite_forum.model.Board;
import com.bite.bite_forum.services.IBoardService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Api(tags = "板块接口")
@RestController
@RequestMapping("/board")
public class BoardController {
    @Resource
    private IBoardService boardService;
    @Value("${bit-forum.index.board-num}")
    private Integer num;

    @ApiOperation("获取首页板块")
    @GetMapping("/topList")
    public AppResult<List<Board>> topList(){
        List<Board> boards = boardService.selectByNum(num);
        if(boards == null){
            boards = new ArrayList<>();
        }
        return AppResult.success(boards);
    }

    @ApiOperation("获取板块信息")
    @GetMapping("getById")
    public AppResult<Board> getById(@ApiParam("板块id") @RequestParam("boardId") @NonNull Long boardId){
        Board board = boardService.selectById(boardId);
        if(board == null || board.getDeleteState() == 1){
            log.info(ResultCode.FAILED_BOARD_NOT_EXISTS.getMessage());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return AppResult.success(board);
    }

}
